package javacource.redis.subscribe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * Create by chenhd on 2021/10/24.
 */
@Component
public class RedisSubConfig {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    RedisSubscribe redisSubscribe;

    /**
     * 订阅主题
     *
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container() {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisTemplate.getConnectionFactory());
        redisMessageListenerContainer.addMessageListener(redisSubscribe, new PatternTopic("decreaseMessage"));
        return redisMessageListenerContainer;
    }

}
